window.SNS = {

    load() {

        if (this.loaded) {
            return;
        }
        this.loaded = true;

        cc.log("验证分享");
        try {

            let res = wx.getLaunchOptionsSync();
            if (res && res.query && res.query.uid) {
                cc.log("成功邀请了！！！！！！！！query:" + res.query.uid);
            }
        } catch (error) {
            cc.error(error);
        }

        //监听
        cc.log("开始监听share");
        try {


            wx.showShareMenu({
                withShareTicket: true,
            });

            let width = cc.winSize.width;
            let height = cc.winSize.height;


            wx.onShareAppMessage(() => {
                if (G.netConfig.wx.shares) {
                    let q = G.netConfig.wx.shares[Math.floor(Math.random() * G.netConfig.wx.shares.length)];
                    return {
                        imageUrlId: q.imageUrlId,
                        imageUrl: q.imageUrl,

                        title: q.title,
                        query: "uid=" + G.uid,
                    }
                }



                return {
                    // imageUrlId: "OLRfZ2qySCmU3adcFZLBkw",
                    // imageUrl: "https://mmocgame.qpic.cn/wechatgame/gKwCzR9cWiaLafIqY6u3q4mnf0KnicfuqLnib7WpBwavU9j77w2QeRKGF25PBoiankZy/0",

                    title: "根本停不下来！！",
                    query: "uid=" + G.uid,
                    imageUrl: cc.game.canvas.toTempFilePathSync({
                        x: 0,
                        y: 0,
                        width: width,
                        height: height,
                        destWidth: width,
                        destHeight: height
                    }),
                }



            });

        } catch (error) {
            cc.error(error);
        }

        try {


            cc.log("监听EVENT_SHOW");
            cc.game.on(cc.game.EVENT_HIDE, () => {
                cc.log("触发EVENT_HIDE");
                if (!this.sharesuccess) {
                    return;
                }

                cc.log("检测到EVENT_HIDE");

                this.shareStartTime = new Date();
            }, this);

            cc.game.on(cc.game.EVENT_SHOW, () => {
                cc.log("触发EVENT_SHOW");
                if (!this.sharesuccess) {
                    return;
                }
                this.shareing = false;


                cc.log("检测到EVENT_SHOW");

                let time = new Date().getTime() - this.shareStartTime.getTime();

                cc.log("超过3秒才算成功,分享间隔时间ms:" + time);
                if (time > 3000) { //
                    cc.log('分享成功')
                    this.sharesuccess();
                } else {
                    cc.log('分享失败')
                    this.sharefail();
                }
                this.sharesuccess = null;

            }, this);
        } catch (e) {
            cc.error(e);

        }
    },




    showAD(forcead) { //显示横幅广告
        try {

            if (this.bannerAd) {
                this.bannerAd.destroy();
            }

            if (!forcead && !G.netConfig.wx.showad) {
                cc.log("是否显示ad", !G.netConfig.wx.showad);
                return;
            }

            cc.log("横幅广告已展示");





            let width = cc.view.getVisibleSize().width;
            let height = cc.view.getVisibleSize().height;


            let design = {
                w: width,
                h: height
            };
            var systemInfo = wx.getSystemInfoSync();
            this.bannerAd = wx.createBannerAd({
                adUnitId: G.netConfig.wx.adUnitId[Math.floor(G.netConfig.wx.adUnitId.length * Math.random())],
                style: {
                    left: (systemInfo.windowWidth - design.w / systemInfo.windowWidth * design.w) / 2,
                    top: systemInfo.windowHeight - design.h / systemInfo.windowHeight * 240,
                    // width: design.w / systemInfo.windowWidth * design.w,
                    // width:systemInfo.windowWidth ,
                    // width:240 ,
                    height: design.h / systemInfo.windowHeight * 240,
                }
            });
            var self = this;
            this.bannerAd.onResize(res => {
                self.bannerAd.style.top = systemInfo.windowHeight - self.bannerAd.style.realHeight;
                self.bannerAd.style.left = (systemInfo.windowWidth - self.bannerAd.style.realWidth) / 2;
                // cc.error("self.bannerAd.style", self.bannerAd.style);

            });


            this.bannerAd.onError(res => {
                cc.error(res.errMsg);
                G.Tip.show("加载横幅广告出错");
            });


            this.bannerAd.show();


            // if (callback) {
            //     callback();
            // }
        } catch (e) {
            cc.error(e);
        }
    },

    hideAD() { //关闭横幅广告
        try {
            cc.log("横幅广告已关闭");
            // if (callback) {
            //     callback();
            // }
            if (this.bannerAd) {
                this.bannerAd.hide();
            }

        } catch (e) {
            cc.error(e);
        }
    },

    showVideo(success, fail) { //显示视频
        try {

            if (!G.netConfig.wx.showvideo) {
                G.Tip.show("广告加载失败");
                return;
            }

            cc.log("视频广告正在创建");

            var self = this;
            if (!self.videoAd) {
                self.videoAd = wx.createRewardedVideoAd({

                    adUnitId: G.netConfig.wx.adUnitIdVideo[Math.floor(G.netConfig.wx.adUnitIdVideo.length * Math.random())],
                })

            }

            // self.videoAd.offClose(callback);
            self.videoAd.onClose(res => {
                self.videoAd.offClose();
                self.videoAd.offError();
                // if (res && res.isEnded || res === undefined) {
                if (res && res.isEnded || res === undefined) {
                    // 成功回掉 callback();
                    cc.error("视频广告播放完毕");

                    success();


                    G.user.dayVideo++;

                    if (G.user.dayVideo > G.netConfig.wx.dayVideo) {
                        G.netConfig.wx.dev = false;
                    }

                } else {
                    cc.log("视频广告没有播放完毕");
                    fail();
                }
            });
            // self.videoAd.offError(callback2);
            self.videoAd.onError(res => {
                self.videoAd.offError();
                cc.error(res.errMsg);
                G.Tip.show("加载广告出错");

                // if (G.netConfig.wx.easy) {
                //     success();
                //     G.user.dayVideo++;

                //     if (G.user.dayVideo > G.netConfig.wx.dayVideo) {
                //         G.netConfig.wx.dev = false;
                //     }
                // }

                if (!G.netConfig.wx.dev) {
                    SNS.share("有人@你", success, fail);
                }

            });



            // });



            self.videoAd.load()
                .then(() => {
                    self.videoAd.show()
                })
                .catch(err => {
                    console.log(err.errMsg)
                    cc.error("show广告异常:" + err.errMsg);

                    G.Tip.show("加载广告异常");
                    if (G.netConfig.wx.easy) {
                        success();

                        G.user.dayVideo++;

                        if (G.user.dayVideo > G.netConfig.wx.dayVideo) {
                            G.netConfig.wx.dev = false;
                        }

                    }

                });




            // self.videoAd.show();

            // this.videoAd.load().then(() => this.videoAd.show());

            // self.videoAd.show()
            //     .catch(err => {
            //         cc.error("show广告异常:" + err.errMsg);
            //         G.Tip.show("加载广告失败");
            //         if (G.netConfig.wx.easy) {
            //             success();


            //             G.user.dayVideo++;

            //             if (G.user.dayVideo > G.netConfig.wx.dayVideo) {
            //                 G.netConfig.wx.dev = false;
            //             }

            //         }

            //         // self.videoAd.load()
            //         //     .then(() => self.videoAd.show())

            //     });


        } catch (e) {
            cc.error(e);

            G.Tip.show("无法加载广告");
            if (G.netConfig.wx.easy) {
                success();


                G.user.dayVideo++;

                if (G.user.dayVideo > G.netConfig.wx.dayVideo) {
                    G.netConfig.wx.dev = false;
                }

            }
            // fail();
        }

    },


    award(success, fail) {

        if (G.netConfig.wx.dev) { //审核模式
            G.Tip.show("加载失败");
            return;
        }

        if (G.netConfig.wx.showvideo) {
            cc.log("视频奖励");
            SNS.showVideo(success, fail == null ? () => {
                G.Tip.show("看完广告获得奖励");
            } : fail);
            return;
        }


        cc.log("开启分享");
        SNS.share("来比比看谁赚的更多~", () => {
            success();
            G.Tip.show("请分享到不同群");
        }, fail == null ? () => {
            G.Tip.show("分享失败,请重试");
        } : fail);


    },




    share(msg, success, fail) {
        try {


            if (G.netConfig.wx.dev) {
                G.Tip.show("加载失败");
                return;
            }



            this.shareing = true;

            this.sharesuccess = success;
            this.sharefail = fail;



            //454
            if (G.netConfig.wx.shares) {
                let q = G.netConfig.wx.shares[Math.floor(Math.random() * G.netConfig.wx.shares.length)];

                wx.shareAppMessage({
                    title: msg,
                    imageUrlId: q.imageUrlId,
                    imageUrl: q.imageUrl,
                    query: "uid=" + G.uid,
                });
                return;
            }




            let width = cc.winSize.width;
            let height = cc.winSize.height;

            //4545
            wx.shareAppMessage({
                title: msg,

                // imageUrlId: "OLRfZ2qySCmU3adcFZLBkw",
                // imageUrl: "https://mmocgame.qpic.cn/wechatgame/gKwCzR9cWiaLafIqY6u3q4mnf0KnicfuqLnib7WpBwavU9j77w2QeRKGF25PBoiankZy/0",

                imageUrl: cc.game.canvas.toTempFilePathSync({
                    x: 0,
                    y: 0,
                    width: width,
                    height: height,
                    destWidth: width,
                    destHeight: height
                }),
                query: "uid=" + G.uid,
            });

        } catch (e) {
            cc.error(e);
            G.Tip.show("分享出错");

            if (G.netConfig.wx.easy) {
                success();
            }

        }

    },
    shareLocal(msg, success, fail) {
        try {

            this.shareing = true;
            this.sharesuccess = success;
            this.sharefail = fail;
            wx.shareAppMessage({
                title: msg,

                imageUrl: canvas.toTempFilePathSync({
                    destWidth: 500,
                    destHeight: 400
                }),

                query: "uid=" + G.uid,
            });

        } catch (e) {
            cc.error(e);
            G.Tip.show("分享出错");
            if (G.netConfig.wx.easy) {
                success();
            }
        }

    },

    showbbs() { //微信游戏圈论坛
        try {
            this.btn = wx.createGameClubButton({
                icon: 'green',
                style: {
                    left: 10,
                    top: 45,
                    width: 30,
                    height: 30,
                }
            });
            this.btn.show();
        } catch (e) {
            cc.error(e);
        }

    },

    hidebbs() {
        try {
            this.btn.hide();
        } catch (e) {
            cc.error(e);
        }
    },


    submit(type, score) {
        try {
            if (window.wx) {
                cc.log("提交分数");
                wx.postMessage({
                    messageType: 3,
                    MAIN_MENU_NUM: type,
                    score: score,
                });
            }
        } catch (e) {
            cc.error(e);
        }
    }

};